const app = getApp();
var config = app.globalData.config;
const http = require('../../utils/http.js');
const jhmd5 = require('../../utils/md5.js');
const jhpay = require('../../utils/pay.js');

Page({

  data: {
    snacks_noon_money: 10,
    snacks_night_money: 10,
    button_status: true,
    index: 0,
    dinner_num: 1,
    array: ['保安室', '学生就餐教室', '其他'],
    spot: [],
    dinner_times: [],
    dinner_total_money: 0,
    thisDate: '',
    calen_show: false,
    noon: true,
    night: false,
    place: '保安室'
  },

  onLoad: function (options) {
    var that = this;
    http.httpPost(config.getSnacksMoney,{},function(e){
      that.setData({
        snacks_noon_money : e.data.snacks_noon,
        snacks_night_money : e.data.snacks_night,
      })
    })
  },

  numInput: function(e){
    this.setData({
      dinner_num : e.detail.value
    })
    this.count_total_money();
  },
  setSnacksOrder: function(){
    var that = this;

    http.httpPost(config.setSnacksOrder,{
      place : that.data.place,
      dinner_times : that.data.dinner_times,
      dinner_num : that.data.dinner_num
    },function(e){
      if(e.code==1){
        that.setData({
          button_status : false
        })
        that.buildBankParam(e.data.order_num,e.data.money);

      }else{
        wx.showToast({
          title: e.msg,
          icon: 'none',
          duration: 2000,
          mask: true
        });
      }
    })

    
  },

  buildBankParam(order_num,total_money){//构建参数
    var that = this;
    var user_openid = app.globalData.userInfo.openid;
    var bankUrl = "";
    
    var _timeStamp = "";
    var _nonceStr = "";
    var _package = "";
    var _signType = "";
    var _paySign = "";
    // console.log("order_num:"+order_num);
    //测试fixme 0.01是金额；倒数第二个是时间
    var bank_str = jhpay.make(order_num,total_money,"","","","","","",user_openid);

    // var _str = "MERCHANTID=105000582994528&POSID=070424517&BRANCHID=350000000&ORDERID=kc202209011147059891&PAYMENT=0.01&CURCODE=01&TXCODE=530590&REMARK1=&REMARK2=&TYPE=1&PUB=65ad01f50fddeaeaf3d7a35d020111&GATEWAY=0&CLIENTIP=120.78.216.181&REGINFO=&PROINFO=&REFERER=&TRADE_TYPE=MINIPRO&SUB_APPID=wx64c7003a659575d7&SUB_OPENID=ofXpS490isnG7fCuIlM3Hit14A_s";
    var bank_mac = jhmd5.hex_md5(bank_str);
    // console.log(bank_mac);
    
    var bankUrl = "https://ibsbjstar.ccb.com.cn/CCBIS/ccbMain?CCB_IBSVersion=V6"+"&"+bank_str+"&MAC="+bank_mac;

    //调用银行接口 todo
    http.httpGet(bankUrl,{},function(e){
      
      var resStatus = e.SUCCESS;
      if(resStatus=="true"){
        var PAYURL = e.PAYURL;
        http.httpGet(PAYURL,{},function(e){
          
          if(e.SUCCESS=="true"){
            _timeStamp = e.timeStamp;
            _nonceStr = e.nonceStr;
            _package = e.package;
            _signType = e.signType;
            _paySign = e.paySign;
            
            that.wxPayBtn(order_num,bank_str,bank_mac,_timeStamp,_nonceStr,_package,_signType,_paySign);
            
          }else{
            wx.showToast({
              title: "支付失败，请稍后再试",
              icon: 'none',
              duration: 1000,
              mask: true
            });
          }
        })
      }
      
    })
      
    
  },

  wxPayBtn(order_num,bank_str,bank_mac,_timeStamp,_nonceStr,_package,_signType,_paySign) { //微信支付
    var that = this;
    wx.requestPayment({
      timeStamp: _timeStamp,
       nonceStr: _nonceStr,
       package: _package,
       signType: _signType,
       paySign: _paySign,
      success: res => {
        setTimeout(function() {
          wx.redirectTo({
            url: '../../pages/index/index'
          })
        }, 200);
      },
      fail: res => {
        that.setData({
          button_status : true
        })
      }
    })
  },


  //计算总金额
  count_total_money: function(){
    let dinner_times = this.data.dinner_times;
    var dinner_total_money = 0;
    //将日期变成数字的形式方便后面进行排序
    for(let i=0;i<dinner_times.length;i++){
      if(dinner_times[i].time == 'noon'){
        dinner_total_money += this.data.snacks_noon_money*this.data.dinner_num;
      }
      if(dinner_times[i].time == 'night'){
        dinner_total_money += this.data.snacks_night_money*this.data.dinner_num;
      }
    }
    this.setData({
      dinner_total_money : dinner_total_money
    })
  },
  //选认选择日期后排序与统计
  calen_hide: function(e){

    let dinner_times = this.data.dinner_times;

    //将日期变成数字的形式方便后面进行排序
    for(let i=0;i<dinner_times.length;i++){
      let str = dinner_times[i].date;
      dinner_times[i].num = parseInt(str.replace(/-/g, ''));
      if(dinner_times[i].time == 'noon'){
        dinner_times[i].num += 0.2;
      }
      if(dinner_times[i].time == 'night'){
        dinner_times[i].num += 0.4;
      }
    }

    //冒泡排序
    for (let g = 0; g < dinner_times.length-1; g++) {
      for (let h = 0; h < dinner_times.length-1; h++) {
        if (dinner_times[h].num > dinner_times[h+1].num) {
          [dinner_times[h],dinner_times[h+1]] = [dinner_times[h+1],dinner_times[h]];
        }
      }
    }

    this.setData({
      calen_show : false,
      dinner_times : dinner_times,
    })
    this.count_total_money();
  },

  calen_show: function(e){
    this.setData({
      calen_show : true
    })
  },

  bindPickerChange: function(e) {  
    console.log(e.detail.value);
    if(e.detail.value==2){
      this.setData({
        index: e.detail.value,
        place: ''
      })
    }else{
      this.setData({
        index: e.detail.value,
        place: this.data.array[e.detail.value]
      })
    }
  },

  placeInput: function(e){
    this.setData({
      place : e.detail.value
    })
  },

  noonChange: function(e){
    let dinner_times = this.data.dinner_times;
    let this_date = this.data.thisDate;
    let spot = [];
    //被选中
    if(e.detail.value.length>0){
      //先查找下数组里原先有没有该条记录
      let t = false;
      for(let i=0;i<dinner_times.length;i++){
        if(dinner_times[i].date==this_date && dinner_times[i].time=='noon'){
          t = true;
        }
      }
      //如果没有再添加进去
      if(!t){
        dinner_times.push({
          date : this_date,
          time : 'noon'
        })
      }
    }else{
      //取消勾选
      let t = false;
      for(let i=0;i<dinner_times.length;i++){
        if(dinner_times[i].date==this_date && dinner_times[i].time=='noon'){
          dinner_times.splice(i,1);
        }
      } 
      for(let i=0;i<dinner_times.length;i++){
        spot.push(dinner_times[i].date);
      }
    }
    for(let i=0;i<dinner_times.length;i++){
      spot.push(dinner_times[i].date);
    }
    this.setData({
      spot : spot,
      dinner_times : dinner_times
    })
  },
  nightChange: function(e){
    let dinner_times = this.data.dinner_times;
    let this_date = this.data.thisDate;
    let spot = [];
    //被选中
    if(e.detail.value.length>0){
      //先查找下数组里原先有没有该条记录
      let t = false;
      for(let i=0;i<dinner_times.length;i++){
        if(dinner_times[i].date==this_date && dinner_times[i].time=='night'){
          t = true;
        }
      }
      //如果没有再添加进去
      if(!t){
        dinner_times.push({
          date : this_date,
          time : 'night'
        })
      }
    }else{
      //取消勾选
      let t = false;
      for(let i=0;i<dinner_times.length;i++){
        if(dinner_times[i].date==this_date && dinner_times[i].time=='night'){
          dinner_times.splice(i,1);
        }
      } 
      for(let i=0;i<dinner_times.length;i++){
        spot.push(dinner_times[i].date);
      }
    }
    for(let i=0;i<dinner_times.length;i++){
      spot.push(dinner_times[i].date);
    }
    this.setData({
      spot : spot,
      dinner_times : dinner_times
    })
  },
  //点击选择日期触发
  dateChange: function(e){
    let noon = false;
    let night = false;
    let this_date = e.detail.dateString;
    this.setData({
      thisDate: this_date
    })
    let dinner_times = this.data.dinner_times;
    for(let i=0;i<dinner_times.length;i++){
      if(dinner_times[i].date==this_date && dinner_times[i].time=='noon'){
        noon = true;
      }
      if(dinner_times[i].date==this_date && dinner_times[i].time=='night'){
        night = true;
      }
    }
    this.setData({
      noon : noon,
      night : night
    })
  }
})